package com.ubtechinc.cruzr.launcher2.widget;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.State;
import android.view.View;

import com.ubtechinc.cruzr.launcher2.utlis.UbtCantans;

/**
 * @author :ruiqiang
 * 翻页RecyclerView对应的DivierDer管理类
 */
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
        super.getItemOffsets(outRect, view, parent, state);
        HorizontalPageLayoutManager layoutManager = (HorizontalPageLayoutManager) parent.getLayoutManager();
        final int childPosition = parent.getChildAdapterPosition(view);
        //间距
        int mSpace = UbtCantans.ITEM_SPACE;
        outRect.set(0, mSpace, 0, 0);
        int columns = layoutManager.getColumns();
        int rows = layoutManager.getRows();
        int pagecount = columns * rows;
        if (layoutManager.isClockFirst()) {
            int newindex = 0;
            if (childPosition > 1) {
                newindex = childPosition + 3;
            }
            int r;
//            if (childPosition < c) {
//                r = childPosition / c;
//            } else {
//                int page = index2 / pagecount;
//                r = (index2 - page * pagecount) / c;
//            }
            if (childPosition >= 2 && childPosition <= columns) {
//                columns = (childPosition - 1) % columns;
                r = 1;
            } else {
                int page = newindex / pagecount;
//                columns = (newindex - page * pagecount) % columns;
                r = (newindex - page * pagecount) / columns;
            }
            if (childPosition == 0 || r == 1) {
                outRect.bottom = mSpace;
            }
            if (childPosition == 0 || childPosition == 1 || r == 0) {
                outRect.top += mSpace;
            }
        } else {
            int page = childPosition / pagecount;
            int r = (childPosition - page * pagecount) / columns;
            if (r != 0) {
                outRect.bottom = mSpace;
            } else {
                outRect.top += mSpace;
            }
        }
    }
}
